/**
 * Project Name:springmgw Maven Webapp
 * File Name:InterfaceController.java
 * Package Name:com.geng.spring4.controller
 * Date:2015年1月13日上午4:42:42
 * Copyright (c) 2015, 上海理想信息产业（集团） All Rights Reserved.
 *
*/

package com.geng.spring4.controller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.geng.imr.service.MessageService;
import com.geng.spring4.model.ReceiveMessage;
import com.geng.spring4.util.WechatProcess;
import com.thoughtworks.xstream.XStream;

/**
 * ClassName:InterfaceController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年1月13日 上午4:42:42 <br/>
 * @author   GengHongChuang
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@RestController
@RequestMapping("/inter")
public class InterfaceController {
    @Autowired
    private MessageService messageService;
    static XStream xstream = new XStream();
    @RequestMapping("msgRevice")
    public void msgRevice(HttpServletRequest request,HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8");  
  
        /** 读取接收到的xml消息 */  
        StringBuffer sb = new StringBuffer();  
        InputStream is = request.getInputStream();  
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");  
        BufferedReader br = new BufferedReader(isr);  
        String s = "";  
        while ((s = br.readLine()) != null) {  
            sb.append(s);  
        }  
        String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据  
        System.out.println(xml);
  
        String result = "";  
        /** 判断是否是微信接入激活验证，只有首次接入验证时才会收到echostr参数，此时需要把它直接返回 */  
        String echostr = request.getParameter("echostr");  
        if (echostr != null && echostr.length() > 1) {  
            result = echostr;  
        } else {  
            System.out.println("weixin:\n"+xml);
            ReceiveMessage receiveMessage = null;
            try {
                xstream.processAnnotations(ReceiveMessage.class);
                receiveMessage = (ReceiveMessage) xstream.fromXML(xml);
                System.out.println("receiveMessage:"+receiveMessage);
                result = messageService.deal(receiveMessage);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
           
           
            //正常的微信处理流程  
           // result = new WechatProcess().processWechatMag(xml);  
            System.out.println(result);
        }  
  
        try {  
            OutputStream os = response.getOutputStream();  
            os.write(result.getBytes("UTF-8"));  
            os.flush();  
            os.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
    //yixin
    @RequestMapping("yichat/msgRevice")
    public void yichatMsgRevice(HttpServletRequest request,HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8");  
        
        /** 读取接收到的xml消息 */  
        StringBuffer sb = new StringBuffer();  
       
        InputStream is = request.getInputStream();  
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");  
        BufferedReader br = new BufferedReader(isr);  
        String s = "";  
        while ((s = br.readLine()) != null) {  
            sb.append(s);  
        }  
        String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据  
        System.out.println("易信信发送xml:"+xml);
  
        String result = "";  
        /** 判断是否是微信接入激活验证，只有首次接入验证时才会收到echostr参数，此时需要把它直接返回 */  
        String echostr = request.getParameter("echostr");  
        if (echostr != null && echostr.length() > 1) {  
            result = echostr;  
            System.out.println(result);
        } else {  
            System.out.println(xml);
            ReceiveMessage receiveMessage = null;
            try {
                xstream.processAnnotations(ReceiveMessage.class);
                receiveMessage = (ReceiveMessage) xstream.fromXML(xml);
                System.out.println("receiveMessage:"+receiveMessage);
                result = messageService.deal(receiveMessage);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
           
           
            //正常的微信处理流程  
           // result = new WechatProcess().processWechatMag(xml);  
            System.out.println(result);
        }  
  
        try {  
            OutputStream os = response.getOutputStream();  
            os.write(result.getBytes("UTF-8"));  
            os.flush();  
            os.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
    
    @RequestMapping(value="feichat/msgRevice",method=RequestMethod.POST)
    public void feichatMsgRevice(HttpServletRequest request,HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8");  
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce =  request.getParameter("nonce");
        String message = request.getParameter("message");
        System.out.println("timestamp:"+timestamp);
        System.out.println("Signature:"+signature);
        System.out.println("nonce:"+nonce);
        System.out.println("message:"+message );
        String echostr1 = request.getParameter("echostr"); 
        
        System.out.println("echostr:"+echostr1);
  
        /** 读取接收到的xml消息 */  
        StringBuffer sb = new StringBuffer(); 
        
        BufferedReader br = request.getReader();
        System.out.println("sb:"+br.readLine());
        /*InputStream is = request.getInputStream();  
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");  
        BufferedReader br = new BufferedReader(isr);  */
        String s = "";  
        while ((s = br.readLine()) != null) {  
           System.out.println(s);
            sb.append(s);  
        }  
        String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据  
        System.out.println("飞信："+xml);
  
        String result = "";  
        /** 判断是否是微信接入激活验证，只有首次接入验证时才会收到echostr参数，此时需要把它直接返回 */  
        String echostr = request.getParameter("echostr");  
        System.out.println(echostr);
        if (echostr != null && echostr.length() > 1) {  
            result = echostr;   
            System.out.println(result);
        } else {  
            System.out.println(xml);
            //正常的微信处理流程  
            result = new WechatProcess().processWechatMag(xml);  
            System.out.println(result);
        }  
  
        try {  
            OutputStream os = response.getOutputStream();  
            os.write(result.getBytes("UTF-8"));  
            os.flush();  
            os.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }

}
